﻿###########################
#
#  Purpose: Replacess The Windows Shell for VMware View Thin Client
#  Created By: Chris Towles
#  When: 11/12/2010
#  Version: 1.0
#  Change List
#
###########################

#Set-ExecutionPolicy -scope Currentuser -executionPolicy Unrestricted -WarningAction SilentlyContinue

$ViewServer = "view.nku.edu"

function VMwareView-ClearLastUsername {

	$key = "hkcu:\\Software\VMware, Inc.\VMware VDM\Client"
	
	#Clear the last user to login to View
	$name = "MRUsername" 
	$RegKey = Get-ItemProperty $key $name -ErrorAction SilentlyContinue
	if($RegKey -ne $null){
		set-itemproperty -path $key -name $name -value $null 
	}
	
	#Detele the key so that it returns to center
	$name = "DialogPos" 
	$RegKey = Get-ItemProperty $key $name -ErrorAction SilentlyContinue
	if($RegKey -ne $null){
		set-itemproperty -path $key -name $name -value $null 
	}
	
	#Minize the ToolBar on the remote
	$name = "InitialPinState" 
	$RegKey = Get-ItemProperty $key $name -ErrorAction SilentlyContinue
	if($RegKey -ne $null){
		set-itemproperty -path $key -name $name -value "FALSE"
	}
}
function VMwareView-StartProcessToFixWindowSize {

	$FixWindowPSFile = "c:\VmwareViewFixWindowSize.ps1"
	if( Test-Path $FixWindowPSFile ){
		Start-Job -FilePath $FixWindowPSFile -Name VMwareViewFixWindowSizeLoop
	}
	else {
		Write-Output "The Powershell file $FixWindowPSFile wasn't found. Its needed to fix the resize issues."
	}
}

Get-Job | Stop-Job | Remove-Job
#| Stop-Job | Remove-Job
#$var = Stop-Job $JobList         # stop any extra jobs still running
#$var = Remove-Job $JobList       # Remove the jobs from the list

#VMwareView-StartProcessToFixWindowSize

$FileToRun = "C:\Program Files\VMware\VMware View\Client\bin\wswc.exe" 
$Arguments = @()
$Arguments += "-serverURL $ViewServer"
$Arguments += "-desktopProtocol PCoIP"
$Arguments += "-loginAsCurrentUser no"
$Arguments += "-desktopLayout fullscreeen"
#$Arguments += "-desktopName `"SL100`""

while($true)
{
	VMwareView-ClearLastUsername
	Start-Process $FileToRun -ArgumentList $Arguments -Wait
}
